.TH "tio" "1" "@version_date@" "tio @version@" "User Commands"

.SH "NAME"
tio \- a serial device I/O tool

.SH "SYNOPSIS"
.PP
.B tio
.RI "[" <options> "] " "<tty-device|profile|tid>"

.SH "DESCRIPTION"
.PP
\fBtio\fR is a serial device tool which features a straightforward command-line
and configuration file interface to easily connect to serial TTY devices for
basic I/O operations.

.SH "OPTIONS"

.TP
.BR \-b ", " "\-\-baudrate " \fI<bps>

Set baud rate [bps] (default: 115200).
.TP
.BR \-d ", " "\-\-databits " 5 | 6 | 7 | 8

Set data bits (default: 8).
.TP
.BR \-f ", " "\-\-flow " hard | soft | none

Set flow control (default: none).
.TP
.BR \-s ", " "\-\-stopbits " 1 | 2

Set stop bits (default: 1).
.TP
.BR \-p ", " "\-\-parity " odd | even | none | mark | space

Set parity (default: none).

Note: With \fBmark\fR parity the parity bit is always 0. With \fBspace\fR
parity the parity bit is always 1. Not all platforms support \fBmark\fR and
\fBspace\fR parity.

.TP
.BR \-o ", " "\-\-output\-delay " \fI<ms>

Set output delay [ms] inserted between each sent character (default: 0).

.TP
.BR \-O ", " "\-\-output\-line\-delay " \fI<ms>

Set output delay [ms] inserted between each sent line (default: 0).

.TP
.BR "    \-\-line\-pulse\-duration " \fI<duration>

Set the pulse duration [ms] of each serial port line using the following key
value pair format in the duration field: <key>=<value>

Each key represents a serial line. The following keys are available:

.RS
.TP 8n
.IP \fBDTR
Data Terminal Ready
.IP \fBRTS
Request To Send
.IP \fBCTS
Clear To Send
.IP \fBDSR
Data Set Ready
.IP \fBDCD
Data Carrier Detect
.IP \fBRI
Ring Indicator
.P
If defining more than one key value pair, the pairs must be comma separated.

The default pulse duration for each line is 100 ms.
.RE

.TP
.BR "\-a, \-\-auto\-connect new|latest|direct"

Automatically connect to serial device according to one of the following
strategies:

.RS
.TP 10n
.IP "\fBnew"
Automatically connect to first new appearing serial device.
.IP "\fBlatest"
Automatically connect to latest registered serial device.
.IP "\fBdirect"
Connect directly to specified TTY device.
.P
All the listed strategies automatically reconnects according to strategy if
device is not available or connection is lost.
.P
Default value is "direct".
.RE

.TP
.BR "    \-\-exclude\-devices \fI<pattern>"

Exclude devices by pattern ('*' and '?' supported).

.TP
.BR "    \-\-exclude\-drivers \fI<pattern>"

Exclude drivers by pattern ('*' and '?' supported).

.TP
.BR "    \-\-exclude\-tids \fI<pattern>"

Exclude topology IDs by pattern ('*' and '?' supported).

.TP
.BR \-n ", " \-\-no\-reconnect

Do not reconnect.

This means that tio will exit if it fails to connect to device or an
established connection is lost.

.TP
.BR \-e ", " "\-\-local\-echo

Enable local echo.

.TP
.BR \-t ", " \-\-timestamp

Enable line timestamp.

.TP
.BR "    \-\-timestamp\-format \fI<format>"

Set timestamp format to any of the following timestamp formats:
.RS
.TP 16n

.IP "\fB24hour"
24-hour format ("hh:mm:ss.sss")
.IP "\fB24hour-start"
24-hour format relative to start time
.IP "\fB24hour-delta"
24-hour format relative to previous timestamp
.IP "\fBiso8601"
ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss")
.IP "\fBepoch"
Seconds since Unix epoch (1970-01-01)
.IP "\fBepoch-usec"
Seconds since Unix epoch (1970-01-01) with subdivision in microseconds
.PP
Default format is \fB24hour\fR
.RE

.TP
.BR "    \-\-timestamp\-timeout \fI<ms>"

Set timestamp timeout value in milliseconds.

This value only takes effect in hex output mode where timestamps are only
printed after elapsed timeout time of no output activity from tty device.

Default value is 200.

.TP
.BR \-l ", " \-\-list

List available targets (serial devices, TIDs, configuration profiles).

.TP
.BR \-L ", " \-\-log

Enable log to file.

The log file will be automatically named using the following format
tio_TARGET_YYYY-MM-DDTHH:MM:SS.log. Target being the command line target such
as tty-device, tid, or configuration profile.

The filename can be manually set using the \-\-log-file option.

.TP
.BR "    \-\-log\-file \fI<filename>

Set log filename.

.TP
.BR "    \-\-log\-directory \fI<path>

Set log directory path in which to save automatically named log files.

.TP
.BR "    \-\-log\-append

Append to log file.

.TP
.BR "    \-\-log-strip

Strip control characters and escape sequences from log.

.TP
.BR \-m ", " "\-\-map " \fI<flags>

Map (replace, translate) characters on input to the serial device or output
from the serial device. The following mapping flags are supported:

.RS
.TP 12n
.IP "\fBICRNL"
Map CR to NL on input (unless IGNCR is set)
.IP "\fBIGNCR"
Ignore CR on input
.IP "\fBIFFESCC"
Map FF to ESC-c on input
.IP "\fBINLCR"
Map NL to CR on input
.IP "\fBINLCRNL"
Map NL to CR-NL on input
.IP "\fBICRCRNL"
Map CR to CR-NL on input
.IP "\fBIMSB2LSB"
Map MSB bit order to LSB on input
.IP "\fBOCRNL"
Map CR to NL on output
.IP "\fBODELBS"
Map DEL to BS on output
.IP "\fBONLCRNL"
Map NL to CR-NL on output
.IP "\fBOLTU"
Map lowercase characters to uppercase on output
.IP "\fBONULBRK"
Map nul (zero) to send break signal on output
.IP "\fBOIGNCR"
Ignore CR on output
.P
If defining more than one flag, the flags must be comma separated.
.RE

.TP
.BR "    \-\-input\-mode " normal|hex|line

Set input mode.

In normal mode input characters are sent immediately as they are typed.

In hex input mode bytes can be sent by typing the \fBtwo-character
hexadecimal\fR representation of the 1 byte value, e.g.: to send \fI0xA\fR you
must type \fI0a\fR or \fI0A\fR.

In line input mode input characters are sent when you press enter. The only
editing feature supported in this mode is backspace.

Default value is "normal".

.TP
.BR "    \-\-output\-mode " normal|hex|hexN

Set output mode.

In hex mode each incoming byte is printed out as a 1 byte hex value.

In hexN mode, N is a number less than or equal to 4096 which defines how many
hex values will be printed before a line break.

Default value is "normal".

.TP
.BR \-c ", " "\-\-color " 0..255|bold|none|list

Colorize tio text using ANSI color code value ranging from 0 to 255 or use
"none" for no color or use "bold" to apply bold formatting to existing system
color.

Use "list" to print a list of available ANSI color codes.

Default value is "bold".

.TP
.BR \-S ", " "\-\-socket \fI<socket>\fR\fB

Redirect I/O to socket.

Any input from clients connected to the socket is sent on the serial port as if
entered at the terminal where tio is running (except that \fBctrl-t\fR sequences
are not recognized), and any input from the serial port is multiplexed to the
terminal and all connected clients.

Sockets remain open while the serial port is disconnected, and writes will block.

Various socket types are supported using the following prefixes in the socket field:

.RS
.TP 20n
.IP "\fBunix:<filename>"
Unix Domain Socket (file)
.IP "\fBinet:<port>"
Internet Socket (network)
.IP "\fBinet6:<port>"
Internet IPv6 Socket (network)
.P
If port is 0 or no port is provided default port 3333 is used.
.P
At present there is a hardcoded limit of 16 clients connected at one time.
.RE

.TP
.BR "    \-\-rs\-485"

Enable RS-485 mode.

.TP
.BR "    \-\-rs\-485\-config " \fI<config>

Set the RS-485 configuration using the following key or key value pair format in
the configuration field:

.RS
.TP 30n
.IP \fBRTS_ON_SEND=value
Set logical level (0 or 1) for RTS pin when sending
.IP \fBRTS_AFTER_SEND=value
Set logical level (0 or 1) for RTS pin after sending
.IP \fBRTS_DELAY_BEFORE_SEND=value
Set RTS delay (ms) before sending
.IP \fBRTS_DELAY_AFTER_SEND=value
Set RTS delay (ms) after sending
.IP \fBRX_DURING_TX
Receive data even while sending data
.P
If defining more than one key or key value pair, they must be comma separated.
.RE

.TP
.BR "\-\-alert none|bell|blink"

Set alert action on connect/disconnect.

It will sound the bell once or blink once on successful connect. Likewise it
will sound the bell twice or blink twice on disconnect.

Default value is "none".

.TP
.BR "\-\-mute"

Mute tio messages.

.TP
.BR "\-\-script \fI<string>

Run script from string.

.TP
.BR "\-\-script\-file \fI<filename>

Run script from file with filename.

.TP
.BR "\-\-script\-run once|always|never"

Run script on connect once, always, or never.

Default value is "always".

.TP
.BR "\-\-exec \fI<command>

Execute shell command with I/O redirected to device

.TP
.BR "\-\-complete-profiles

Prints profiles (for shell completion)
.TP
.BR \-v ", " \-\-version

Display program version.
.TP
.BR \-h ", " \-\-help

Display help.
.SH "KEY COMMANDS"
.PP
.TP 16n
In session, all key strokes are forwarded to the serial device except the following key sequence: a prefix key (default: ctrl-t) followed by a command key. These sequences are intercepted as key commands:
.IP "\fBctrl-t ?"
List available key commands
.IP "\fBctrl-t b"
Send serial break (triggers SysRq on Linux, etc.)
.IP "\fBctrl-t c"
Show configuration (baudrate, databits, etc.)
.IP "\fBctrl-t e"
Toggle local echo mode
.IP "\fBctrl-t f"
Toggle log to file
.IP "\fBctrl-t F"
Flush data I/O buffers (discard data written but not transmitted and data received but not read)
.IP "\fBctrl-t g"
Toggle serial port line
.IP "\fBctrl-t i"
Toggle input mode
.IP "\fBctrl-t l"
Clear screen
.IP "\fBctrl-t L"
Show line states (DTR, RTS, CTS, DSR, DCD, RI)
.IP "\fBctrl-t m"
Change mapping of characters on input or output
.IP "\fBctrl-t o"
Toggle output mode
.IP "\fBctrl-t p"
Pulse serial port line
.IP "\fBctrl-t q"
Quit
.IP "\fBctrl-t r"
Run script
.IP "\fBctrl-t R"
Execute shell command with I/O redirected to device
.IP "\fBctrl-t s"
Show TX/RX statistics
.IP "\fBctrl-t t"
Toggle line timestamp mode
.IP "\fBctrl-t v"
Show version
.IP "\fBctrl-t x"
Send file using the XMODEM-1K or XMODEM-CRC protocol (prompts for file name and protocol)
.IP "\fBctrl-t y"
Send file using the YMODEM protocol (prompts for file name)
.IP "\fBctrl-t ctrl-t"
Send ctrl-t character

.SH "SCRIPT API"
.PP
Tio suppots Lua scripting to easily automate interaction with the tty device.

In addition to the standard Lua API tio makes the following functions
and variables available:

.TP 6n

.IP "\fBtio.expect(pattern, timeout)"
Waits for the Lua pattern to match or timeout before continuing.
Timeout is in milliseconds, defaults to 0 meaning it will wait forever.

Returns the captures from the pattern or nil on timeout.

.IP "\fBtio.read(size, timeout)"
Read up to size bytes from serial device. If timeout is 0 or not provided it
will wait forever until data is ready to read.

Returns a string up to size bytes long on success and nil on timeout.

.IP "\fBtio.readline(timeout)"
Read line from serial device. If timeout is 0 or not provided it will wait
forever until data is ready to read.

Returns a string on success and nil on timeout.  On timeout a partially read
line may be returned as a second return value.

.IP "\fBtio.write(string)"
Write string to serial device.

Returns the tio table.

.IP "\fBtio.send(file, protocol)"
Send file using x/y-modem protocol.

Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.

.IP "\fBtio.ttysearch()"
Search for serial devices.

Returns a table of number indexed tables, one for each serial device found.
Each of these tables contains the serial device information accessible via the
following string indexed elements "path", "tid", "uptime", "driver",
"description".

Returns nil if no serial devices are found.

.IP "\fBtio.set{line=state, ...}"
Set state of one or multiple tty modem lines.

Line can be any of DTR, RTS, CTS, DSR, CD, RI

State is high, low, or toggle.

.IP "\fBtio.sleep(seconds)"
Sleep for seconds.
.IP "\fBtio.msleep(ms)"
Sleep for milliseconds.

.IP "\fBtio.alwaysecho"
A boolean value, defaults to true.

If tio.alwaysecho is false, the result of tio.read, tio.readline or tio.expect
will only be returned from the function and not logged or printed.

If tio.alwaysecho is set to true, reading functions also emit a single
timestamp to stdout and log file per options.timestamp and options.log.

.SH "CONFIGURATION FILE"
.PP
Options can be set via configuration file using the INI format. \fBtio\fR uses
the configuration file first found in the following locations in the order
listed:

.PP
.I $XDG_CONFIG_HOME/tio/config
.PP
.I $HOME/.config/tio/config
.PP
.I $HOME/.tioconfig

.PP
Labels can be used to group settings into named configuration profiles which
can be activated from the command-line when starting tio.

.PP
\fBtio\fR will try to match the user input to a configuration profile by name or by
pattern to get the TTY device and other options.

.PP
Options without any label change the default options.

.PP
Any options set via command-line will override options set in the configuration file.

.PP
The following configuration file options are available:

.TP 25n
.IP "\fBpattern"
Pattern matching user input. This pattern can be an extended regular expression with a single group.
.IP "\fBdevice"
TTY device to open. If it contains a "%s" it is substituted with the first group match.
.IP "\fBbaudrate"
Set baud rate
.IP "\fBdatabits"
Set data bits
.IP "\fBflow"
Set flow control
.IP "\fBstopbits"
Set stop bits
.IP "\fBparity"
Set parity
.IP "\fBoutput-delay"
Set output character delay
.IP "\fBoutput-line-delay"
Set output line delay
.IP "\fBline-pulse-duration"
Set line pulse duration
.IP "\fBno-reconnect"
Do not reconnect
.IP "\fBlog"
Enable log to file
.IP "\fBlog-file"
Set log filename
.IP "\fBlog-directory"
Set log directory path in which to save automatically named log files.
.IP "\fBlog-append"
Append to log file
.IP "\fBlog-strip"
Enable strip of control and escape sequences from log
.IP "\fBlocal-echo"
Enable local echo
.IP "\fBtimestamp"
Enable line timestamp
.IP "\fBtimestamp-format"
Set timestamp format
.IP "\fBtimestamp-timeout"
Set timestamp timeout
.IP "\fBmap"
Map characters on input or output
.IP "\fBcolor"
Colorize tio text using ANSI color code ranging from 0 to 255
.IP "\fBinput-mode"
Set input mode
.IP "\fBoutput-mode"
Set output mode
.IP "\fBsocket"
Set socket to redirect I/O to
.IP "\fBprefix-ctrl-key"
Set prefix ctrl key (a..z or 'none', default: t)
.IP "\fBrs-485"
Enable RS-485 mode
.IP "\fBrs-485-config"
Set RS-485 configuration
.IP "\fBalert"
Set alert action on connect/disconnect
.IP "\fBmute"
Mute tio messages
.IP "\fBscript"
Run script from string
.IP "\fBscript-file"
Run script from file
.IP "\fBscript-run"
Run script on connect
.IP "\fBexec"
Execute shell command with I/O redirected to device

.PP
It is possible to include the content of other configuration files using the
include directive like so: "[include <file>]".

.SH "CONFIGURATION FILE EXAMPLES"

.TP
To change the default configuration simply set options like so:

.RS
.nf
.eo
[default]
baudrate = 9600
databits = 8
parity = none
stopbits = 1
color = 10
line-pulse-duration = DTR=200,RTS=400
.ec
.fi
.RE

.TP
Named configuration profiles can be added via labels:

.RS
.nf
.eo
[rpi3]
device = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0
baudrate = 115200
color = 11
.ec
.fi
.RE

.TP
Activate the configuration profile by name:

$ tio rpi3

.TP
Which is equivalent to:

$ tio -b 115200 -c 11 /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0

.TP
A configuration profile can also be activated by its pattern which supports regular expressions:

.RS
.nf
.eo
[usb-devices]
pattern = ^usb([0-9]*)
device = /dev/ttyUSB%m1
baudrate = 115200
.ec
.fi
.RE

.TP
Activate the configuration profile by pattern match:

$ tio usb12

.TP
Which becomes equivalent to:

$ tio -b 115200 /dev/ttyUSB12

.TP
It is also possible to combine use of configuration profile and command-line options. For example:

$ tio -l -t usb12

.SH "EXAMPLES"
.TP
Typical use is without options:

$ tio /dev/ttyUSB0
.TP
Which corresponds to the commonly used default options:

$ tio \-b 115200 \-d 8 \-f none \-s 1 \-p none /dev/ttyUSB0
.TP
It is recommended to connect serial TTY devices by ID:

$ tio /dev/serial/by\-id/usb\-FTDI_TTL232R-3V3_FTGQVXBL\-if00\-port0
.PP
Using serial devices by ID ensures that tio automatically reconnects to the
correct serial device if it is disconnected and then reconnected.
.TP
Redirect serial device I/O to Unix file socket for scripting:

$ tio -S unix:/tmp/tio-socket0 /dev/ttyUSB0

.TP
Then, to issue a command via the file socket simply do:

$ echo "ls -la" | nc -UN /tmp/tio-socket0 > /dev/null

.TP
Or use the expect command to script an interaction:

.RS
.nf
.eo
#!/usr/bin/expect -f
.sp
set timeout -1
log_user 0
.sp
spawn nc -UN /tmp/tio-socket0
set uart $spawn_id
.sp
send -i $uart "date\n"
expect -i $uart "prompt> "
send -i $uart "ls -la\n"
expect -i $uart "prompt> "
.ec
.fi
.RE

.TP
It is also possible to use tio's own simpler expect/send script functionality to e.g. automate logins:

$ tio --script 'tio.expect("login: "); tio.write("root\\n"); tio.expect("Password: "); tio.write("root\\n")' /dev/ttyUSB0

.TP
Redirect device I/O to network file socket for remote TTY sharing:

$ tio --socket inet:4444 /dev/ttyUSB0

.TP

Then, use netcat to connect to the shared TTY session over network (assuming tio is hosted on IP 10.0.0.42):

$ nc -N 10.0.0.42 4444

.TP
Pipe command to the serial device:

$ echo "ls -la" | tio /dev/serial/by\-id/usb\-FTDI_TTL232R-3V3_FTGQVXBL\-if00\-port0

.TP
Pipe command to serial device and wait for line response within 1 second:

$ echo "*IDN?" | tio /dev/ttyACM0 --script "tio.expect('\\r\\n', 1000)" --mute
.TP

.TP
Likewise, to pipe data from file to the serial device:

$ cat data.bin | tio /dev/serial/by\-id/usb\-FTDI_TTL232R-3V3_FTGQVXBL\-if00\-port0

.TP
Map NL to CR-NL on input from device and DEL to BS on output to device:

$ tio --map INLCRNL,ODELBS /dev/ttyUSB0

.TP
Enable RS-485 mode:

$ tio --rs-485 --rs-485-config=RTS_ON_SEND=1,RX_DURING_TX /dev/ttyUSB0

.TP
Manipulate DTR and RTS lines upon first connect to reset connected microcontroller:

$ tio --script "tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{RTS=toggle}" --script-run once /dev/ttyUSB0

.SH "WEBSITE"
.PP
Visit https://tio.github.io

.SH "AUTHOR"
.PP
Maintained by Martin Lund <martin.lund@keep\-it\-simple.com>.
